#!/usr/bin/python3

import sys
from cgi import escape

open_els = []

attr_ok = False

output = open('/dev/stdout', 'w')

def indent():
    return ' ' * (len(open_els)-1)

def out(data):
    global output
    output.write(data)

def open_el(el):
    global attr_ok

    finalize_el()
    open_els.append(el)
    out('{}<{}'.format(indent(), el))
    attr_ok = True

def add_attr(attr):
    out(' {}'.format(attr))

def single_el(el, content=None):
    open_el(el)
    finalize_el(newline=False)
    if content:
        out(content)
    close_el(oneliner=True)

def finalize_el(newline=True):
    global attr_ok

    if attr_ok:
        out(">{}".format("\n" if newline else ''))
    attr_ok = False

def close_el(oneliner=False):
    global attr_ok

    el = open_els.pop()
    if attr_ok:
        out("/>\n")
    else:
        out("{}</{}>\n".format('' if oneliner else indent(), el))
    attr_ok = False

for opt in sys.argv[1:]:
    cmd = opt[0]
    if cmd in ['<', '+', '-', '@', '.']:
        if cmd == '<':
            ''' raw output '''
            attr_ok = False
            out(opt + "\n")
        elif cmd == '+':
            ''' open a new element '''
            open_el(opt[1:])
        elif cmd == '-':
            levels = 0
            while opt[levels] == '-':
                close_el()
                levels += 1
            open_el(opt[levels:])
        elif cmd == '@':
            if not attr_ok:
                raise ValueError('@ not allowed after text or raw')
            add_attr(opt[1:])
        elif cmd == '.':
            el = None
            content = None
            if opt.find('=') > 0 and opt.find('=') < opt.find(' ') or (opt.find('=')>0 and opt.find(' ') < 0):
                (el, content) = opt[1:].split('=', 1)
            else:
                el = opt[1:]
            single_el(el, content)

    else:
        
        if opt.find('=') > 0 and opt.find('=') < opt.find(' ') or (opt.find('=')>0 and opt.find(' ') < 0):
            finalize_el()
            single_el(*opt.split('=', 1))
        else:
            finalize_el(newline=False)
            out(escape(opt))
            close_el(oneliner=True)
        attr_ok = False

while len(open_els):
    close_el()

output.close()
